﻿@model Controller2.Models.Pessoa_PF
@{
    Layout = null;
}
<!DOCTYPE html>
<html class="no-js">
<head>
    <title>Cadastro de Cliente: Pessoa Física</title>
    @if (!Request.IsAjaxRequest())
    {
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link href="../../Content/themes/controller/controller.css" rel="stylesheet" type="text/css" />
        @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))  
        <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>    
        <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    }
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/controller.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/numbers.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))
    @if (false)
    {
        <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
        <script src="../../Scripts/controller.js" type="text/javascript"></script>
    }
    <style type="text/css">
        .editor-label
        {
            width: 140px;
        }
        #NomePessoa
        {
            width: 350px;
        }
        editor-field input[type=radio]
        {
            display: inline-block;
            position: relative;
        }
        #IdEstado
        {
            border: 1px solid #ff0000;
        }
    </style>
    @{
        var idEstado = Model.Cidade != null ? Model.Cidade.IdEstado.ToString() : "";
        var newUser = Model.UserName.Equals("") ? true : false;
        var newCli = Model.IdPessoa > 0 ? false : true;
    }
</head>
<body id="frame">
    <div id="formHide">
        @using (Html.BeginForm("SalvarClientePF", "Cliente", FormMethod.Post))
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Pessoa Física</legend>
                @Html.HiddenFor(model => model.IdPessoa)
                <div class="editor-label">
                    @Html.LabelFor(model => model.DataCadastro, "Data de Cadastro: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.DataCadastro)
                    @Html.ValidationMessageFor(model => model.DataCadastro)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.DataNascimento, "Data de Nascimento: ")
                    @Html.EditorFor(model => model.DataNascimento)
                    @Html.ValidationMessageFor(model => model.DataNascimento)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.NomePessoa, "Nome: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.NomePessoa)
                    @Html.ValidationMessageFor(model => model.NomePessoa)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Sexo, "Sexo: ")
                </div>
                <div class="editor-field">
                    @Html.RadioButton("Sexo", "M", true, new { id = "Masc" })<label for="Masc"><img src="@Url.Content("~/Content/imagens/male.png")" alt="Masculino">
                        Masculino</label>
                    &nbsp;&nbsp;&nbsp;
                    @Html.RadioButton("Sexo", "F", new { id = "Fem" })<label for="Fem"><img src="@Url.Content("~/Content/imagens/female.png")" alt="Feminino">Feminino</label>
                    @Html.ValidationMessageFor(model => model.Sexo)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.RG, "RG: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.RG)
                    @Html.ValidationMessageFor(model => model.RG)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.CPF, "CPF: ")
                    @Html.EditorFor(model => model.CPF)
                    @Html.ValidationMessageFor(model => model.CPF)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Endereco, "Endereço: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Endereco)
                    @Html.ValidationMessageFor(model => model.Endereco)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.NumEnd, "Nº: ")
                    @Html.EditorFor(model => model.NumEnd)
                    @Html.ValidationMessageFor(model => model.NumEnd)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Complemento, "Complemento: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Complemento)
                    @Html.ValidationMessageFor(model => model.Complemento)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Bairro, "Bairro: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Bairro)
                    @Html.ValidationMessageFor(model => model.Bairro)
                    @Html.LabelFor(model => model.CEP, "CEP: ")
                    @Html.EditorFor(model => model.CEP)
                    @Html.ValidationMessageFor(model => model.CEP)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdEstado, "Estado: ")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.IdEstado, (IEnumerable<SelectListItem>)ViewBag.Estado, "", new { style = "width: 200px;" })
                    @Html.ValidationMessageFor(model => model.IdEstado)
                    &nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.IdCidade, "Cidade: ")
                    @if (Model.IdCidade > 0)
                    {
                        @Html.DropDownListFor(model => model.IdCidade, (IEnumerable<SelectListItem>)ViewBag.Cidade, "", new { style = "width: 252px;" })
                    }
                    else
                    {
                        @Html.DropDownListFor(model => model.IdCidade, (IEnumerable<SelectListItem>)ViewBag.Cidade, "", new { style = "width: 252px;", disabled = "disabled" })
                    }
                    @Html.ValidationMessageFor(model => model.IdCidade)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Fone, "Fone: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Fone)
                    @Html.ValidationMessageFor(model => model.Fone)
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    @Html.LabelFor(model => model.Celular, "Celular: ")
                    @Html.EditorFor(model => model.Celular)
                    @Html.ValidationMessageFor(model => model.Celular)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Site, "Site: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Site)
                    @Html.ValidationMessageFor(model => model.Site)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Email, "E-mail: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Twitter, "Twitter: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Twitter)
                    @Html.ValidationMessageFor(model => model.Twitter)
                </div>
                @if (!newUser)
                {
                    <div class="editor-label">
                        @Html.LabelFor(model => model.UserName, "Usuário: ")
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.UserName, new { disabled = "disabled", style = "width: 400px;" })
                    </div>
                }
            </fieldset>
            <div id="botoesForm">
                @if (!newUser)
                {
                    <input type="button" id="ExcluirUsuario" value="Excluir Usuário" />
                }
                @{var textoGravar = "Gravar e criar usuário";}
                @if (Model.IdPessoa > 0 && !Model.UserName.Equals(""))
                {
                    textoGravar = "Gravar";
                }
                <input type="submit" id="Gravar" value="@textoGravar" />
                <input type="button" id="Fechar" value="Cancelar" />
            </div>
        }
    </div>
    <script type="text/javascript">
        var novo = ('@newCli'.toLowerCase() === 'true');
        var cpfAntigo = '@Model.CPF';
        $.validator.methods.existeCpf = function (value, element, param) {
            showLoading();
            var ok = true;
            if (cpfAntigo != $(element).val()) {
                $.ajax({
                    url: '@Url.Action("ExisteCPF", "ValidacoesRemotas")',
                    type: 'GET',
                    async: false,
                    data: { cpf: $(element).val() },
                    error: function (x, e, thrownError) {
                        hideLoading();
                        alertError('Erro ' + x + ': ' + thrownError);
                        ok = false;
                    },
                    success: function (data) {
                        hideLoading();
                        if (data.existe) {
                            alertError(data.error);
                            ok = false;
                        } else {
                            ok = true;
                        }
                    }
                });
            }
            return ok;
        };
        $(function () {
            $('#formHide').fadeIn('slow');
            $('#IdEstado').rules('add', 'required');
            if (novo) {
                $('#DataNascimento').empty();
            }
            $('.datepicker').mask("99/99/9999");
            $("#Fone").mask("(99) 9999-9999");
            $("#Celular").mask("(99) 9999-9999");
            $("#CPF").mask("999.999.999-99");
            $("#CPF").rules('add', { existeCpf: true, messages: { existeCpf: "*"} });
            $("#CEP").mask("99999-999");
            hideLoading();
            if (novo) {
                $('#DataNascimento').focus();
            } else {
                $('#NomePessoa').focus();
            }
        });
        $('form').submit(function (e) {
            e.preventDefault();
            var datavalida = true;
            $('.datepicker').each(function () {
                if (!validaData(this)) {
                    datavalida = false;
                    $(this).focus();
                }
            });
            if (datavalida) {
                if ($('form').valid()) {
                    showLoading();
                    $.post('@Url.Action("SalvarClientePF", "Cliente")', $('form').serialize(), function (data) {
                        $('#Gravar').attr('disabled', false);
                        if (data.salvo) {
                            if (novo) {
                                window.location.href = '@Url.Action("IncluirUsuario", "Account")/' + data.id + '?request=Cliente&email=' + $('#Email').val();
                            } else if (data.username == '') {
                                window.location.href = '@Url.Action("IncluirUsuario", "Account")/' + data.id + '?request=Cliente&email=' + $('#Email').val();
                            } else {
                                closeWindowPFPJ();
                            }
                        } else {
                            alertError(data.error);
                        }
                        hideLoading();
                    });
                }
            }
        });
        $('#Fechar').click(function () {
            closeWindowPFPJ();
        });
        $('#IdEstado').change(function (e) {
            showLoading();
            var idEstado = $('#IdEstado option:selected').val();
            if (idEstado == "") {
                $('#IdCidade').empty();
                $('#IdCidade').attr('disabled', 'disabled');
            } else {
                $('#IdCidade').empty();
                $('#IdCidade').append('<option>Carregando...</option>');
                var url = '@Url.Action("CarregarComboCidade", "Cliente")';
                $.post(url, { idEstado: idEstado }, function (dados) {
                    $('#IdCidade').empty();
                    $.each(dados, function (index, cidade) {
                        $('#IdCidade').append('<option value="' + cidade.Value + '">' + cidade.Text + '</option>');
                    });
                    $('#IdCidade').removeAttr('disabled').focus();
                });
            }
            hideLoading();
        });
        $('.datepicker').blur(function () {
            if (!validaData(this)) {
                $(this).focus();
            }
        });
        $('#ExcluirUsuario').click(function () {
            confirmDialog('confirmarExclusao', 'excluirUser', '', '', '');
        });
        function excluirUser() {
            showLoading();
            var user = $('#UserName').val();
            $.post('@Url.Action("ExcluirUsuario", "Account")', { username: user }, function (data) {
                if (data.salvo) {
                    $('#UserName').val('');
                    $('#ExcluirUsuario').remove();
                    $('#Gravar').val("Gravar e criar usuário");
                } else {
                    alertError(data.error);
                }
                hideLoading();
            });
        }
    </script>
</body>
</html>
